package com.junson.client.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.junson.client.EchoClient;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * 代码清单 2-3 客户端的 ChannelHandler
 *
 * @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a>
 */
@Sharable
//标记该类的实例可以被多个 Channel 共享
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
	private static Logger logger = LoggerFactory.getLogger(EchoClient.class);

    @Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    	
        //将消息记录到控制台
        logger.debug("client received==>" + msg.toString());
		
	}

    @Override
    //在发生异常时，记录错误并关闭Channel
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
